//
//  KKVisitorLoginView.swift
//  SinaWeiBo
//
//  Created by King.Kong. on 16/5/23.
//  Copyright © 2016年 King.Kong. All rights reserved.
//

import UIKit

import SnapKit

// MARK:1.制作协议
@objc protocol KKVisitorLoginViewDelegate: NSObjectProtocol {
    
    // MARK:2.声明方法
    optional func userWillLogin(btn:UIButton)
   optional func userWillRegister()
    
}


class KKVisitorLoginView: UIView {

    // MARK: 3.声明代理属性
    weak var visitorDlegate:KKVisitorLoginViewDelegate?
    
    // MARK: 通过此方法让外界调用访客视图中的视图元素
    func updateUI(tipTitle:String,imageName:String,isHome:Bool = false) {
        
        if isHome {
            
            startAnimation()
        } else {
            
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
            circleIcon.image = UIImage(named:imageName)
            tipLable.text = tipTitle
        }
        
    }
    
    // MARK: 给圈圈添加动画效果
    private func startAnimation() {
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        basic.duration = 15
        basic.repeatCount = MAXFLOAT
        basic.toValue = 2 * M_PI
        // 让layer不会由于视图消失而停止
        basic.removedOnCompletion = false
        circleIcon.layer.addAnimation(basic, forKey: nil)
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 加载UI界面
    private func setupUI() {
        
        addSubview(circleIcon)
        addSubview(largeHouse)
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        addSubview(coverIcon)
        
        circleIcon.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon)
        }
        
        tipLable.snp_makeConstraints { (make) in
            make.centerX.equalTo(circleIcon)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.height.equalTo(40)
            make.width.equalTo(230)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(tipLable)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
            
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLable)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        coverIcon.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon)
        }
        
        // 使用sRGB取值
        let value: CGFloat = 237.0 / 255.0
        
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
        
        loginBtn.addTarget(self, action: #selector(KKVisitorLoginView.loginBtnDidClick), forControlEvents: .TouchUpInside)
        
        registerBtn.addTarget(self, action: #selector(KKVisitorLoginView.registerBtnDidClick), forControlEvents: .TouchUpInside)

    }
    
    let oauthViewController: KKOAuthViewController = KKOAuthViewController()
    
    @objc private func loginBtnDidClick() {
       
        // MARK:4.代理属性实现代理方法
        visitorDlegate?.userWillLogin?(self.loginBtn)
        
        
    }
    @objc private func registerBtnDidClick() {
        
        visitorDlegate?.userWillRegister?()
        
    }
   // 懒加载子视图
    // 遮罩视图
    private lazy var coverIcon:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    // 圈圈
    private lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    // 大房子
    private lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    
    // 文案
    private lazy var tipLable: UILabel = UILabel(title: "关注一些人,回到这里看看有什么惊喜", textColor: UIColor.darkGrayColor(), font: 14)
        
    // 登陆按钮
    private lazy var loginBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.backgroundColor = UIColor.redColor()
        return btn
    }()
    
    // 注册按钮
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return btn
    }()
    
}

